import Vue from 'vue'
import Router from 'vue-router'
import userRoutes from './module/cgp'
import _import_ from './importViews'
Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: 'views',
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'login',
      component: _import_('Login'),
      meta: {
        title: '登录',
        subRoot: false
      }
    },
    {
      path: '/home',
      name: 'home',
      component: _import_('Home'),
      children: [
        ...userRoutes
      ]
    },
    {
      path: '/404',
      name: '404',
      component: _import_('common/PageNotFund')
    },
    {
      path: '*',
      redirect: '/404'
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    sessionStorage.removeItem('user')
  }
  const user = JSON.parse(sessionStorage.getItem('user'))
  if (!user && to.path !== '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
export default router
